package com.batata.continuing.service.user.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.batata.continuing.dao.user.UserDao;
import com.batata.continuing.pojo.base.ResultData;
import com.batata.continuing.pojo.user.UserInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.batata.continuing.pojo.user.UserTest;
import com.batata.continuing.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;




@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, UserInfo> implements UserService {


    @Autowired
    private UserDao userDao;

    /**
     * 查询用户的数量
     *
     * @return
     */
    @Override
    public long getUserCount() {
        return this.baseMapper.getUserCount();
    }

    @Override
    public ResultData<?> selectTest() {
        UserTest userTest = userDao.selectTest();
        System.out.println("map = " + userTest.toString());
        return ResultData.success(userTest);
    }


    public ResultData<?> createUser(UserInfo userInfo) {

        // 是否存在相同的登陆名 start
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userInfo.getLoginName()).or().eq("phone_number", userInfo.getPhoneNumber());
        boolean exists = lambdaQuery()
                .eq(UserInfo::getLoginName, userInfo.getLoginName())
                .or()
                .eq(UserInfo::getPhoneNumber, userInfo.getPhoneNumber())
                .exists();
        if (exists) {
            return ResultData.error( "登陆名或手机号码重复");
        }
        // 是否存在相同的登陆名 end

        // 性别判断   start
        if (
                !StringUtils.isEmpty(userInfo.getSex())
        ) {
            if (userInfo.getSex().equals("0")) {
                userInfo.setSexText("男");
            } else if (userInfo.getSex().equals("1")) {
                userInfo.setSexText("女");
            } else {
                return ResultData.error("性别参数不合法");
            }
        }
        // 性别判断   end


        int insert = 0;
        try {
            insert = this.baseMapper.insert(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultData.success();
    }


    /**
     * 是否存在 loginName true 存在 false 不存在
     *
     * @param loginName 登录名 账号
     * @return true 存在 false 不存在
     */
    public boolean isHaveByLoninName(String loginName) {
        return isHaveByLoninNameOrLoginPassword(loginName, "login_name");
    }

    /**
     * 是否存在 loginPassword true 存在 false 不存在
     *
     * @param phoneNumber 登录密码
     * @return true 存在 false 不存在
     */
    public boolean isHaveByPhoneNumber(String phoneNumber) {
        return isHaveByLoninNameOrLoginPassword(phoneNumber, "phone_number");
    }

    /**
     * 是否存在 根据 value 和 field
     *
     * @param value 值
     * @param field 字段
     * @return true 存在 false 不存在
     */
    public boolean isHaveByLoninNameOrLoginPassword(String value, String field) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(field, value);
        return this.baseMapper.selectCount(queryWrapper) > 0;
    }


}
